﻿using System;
using System.Collections.Generic;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using EbbinghausWord.Entities;
using System.Linq;


namespace EbbinghausWord.BLL
{
    /// <summary>
    /// 队列管理
    /// </summary>
    public static class WordQueueManager
    {
        #region private field
        private static Queue<WordEntity> _queue = new Queue<WordEntity>();
        #endregion

        #region public method
        /// <summary>
        /// 将列表中的元素添加到队列中
        /// </summary>
        /// <param name="wordList"></param>
        public static Queue<WordEntity> AddToQueue(this List<WordEntity> wordList)
        {
            foreach (var wordEntity in wordList)//.OrderBy(x => x.OrderRandom))
            {
                _queue.Enqueue(wordEntity);
            }

            return _queue;
        }

        /// <summary>
        /// 将列表中指定数量的元素添加到队列中，不足count则添加最大数量
        /// </summary>
        /// <param name="wordList"></param>
        /// <param name="count"></param>
        public static Queue<WordEntity> AddToQueue(this List<WordEntity> wordList, int count)
        {
            var timer = 0;
            foreach (var wordEntity in wordList.Take(count)) //.OrderBy(x => x.OrderRandom))
            {
                _queue.Enqueue(wordEntity);
                //if (++timer == count) break;
            }

            return _queue;
        }

        /// <summary>
        /// 入队
        /// </summary>
        /// <param name="wordEntity"></param>
        /// <returns></returns>
        public static void Enqueue(WordEntity wordEntity)
        {
            _queue.Enqueue(wordEntity);
        }

        /// <summary>
        /// 出队，如果队列中没有元素，则返回null
        /// </summary>
        public static WordEntity Dequeue()
        {
            return _queue.Count > 0 ? _queue.Dequeue() : null;
        }

        /// <summary>
        /// 返回队列的第一个元素，但是该元素不出队列 
        /// </summary>
        /// <returns></returns>
        public static WordEntity Peek()
        {
            return _queue.Count > 0 ? _queue.Peek() : null;
        }

        /// <summary>
        /// 初始化队列
        /// </summary>
        public static void InitQueue()
        {
            _queue = IsExistQueueFile() ? LoadQueueFile()
                : new Queue<WordEntity>();
        }

        /// <summary>
        /// 队列中是否包含某个成员
        /// </summary>
        /// <returns></returns>
        public static bool Contains(WordEntity wordEntity)
        {
            return _queue.Contains(wordEntity);
        }

        #endregion

        #region 队列文件操作

        /// <summary>
        /// 是否存在队列文件
        /// </summary>
        /// <returns></returns>
        private static bool IsExistQueueFile()
        {
            return StoreManager.IsFileExist(SystemConfig.QueueFileName);
        }

        ///<summary>
        ///加载队列文件，建立与用户单词本的引用关系，并删除队列文件
        ///</summary>
        ///<returns></returns>
        private static Queue<WordEntity> LoadQueueFile()
        {
            var wordList = StoreManager.OpenFile(SystemConfig.QueueFileName);
            var queue = new Queue<WordEntity>();
            //这里是为了建立映引用关系
            foreach (var wordEntity in wordList)
            {
                queue.Enqueue(WordBookManager.GetWordByEnglish(wordEntity.English));
            }

            //删除队列文件
            StoreManager.DeleteFile(SystemConfig.QueueFileName);
            return queue;
        }

        /// <summary>
        /// 保存队列文件
        /// </summary>
        public static void SaveQueueFile()
        {
            _queue.SaveUserWordBookFile(SystemConfig.QueueFileName);
        }
        #endregion

        #region public property

        /// <summary>
        /// 队列所包含的成员数量
        /// </summary>
        public static int Count
        {
            get { return _queue.Count; }
        }
        #endregion
    }
}
